package game.players.states;

import java.util.ArrayList;
import java.util.List;

import game.ball.SoccerBall;
import game.players.SoccerPlayer;

public abstract class WithBall extends PlayerState {
	
	private List<SoccerPlayer> teammates = new ArrayList<SoccerPlayer>();
	private SoccerBall soccerBall;

	public WithBall(SoccerPlayer player) {
		super(player);
		this.setSoccerBall(player.getScene().getSoccerBall());
	}
	
	public WithBall(SoccerPlayer player, SoccerBall ball) {
		super(player);
		this.setSoccerBall(ball);
	}

	@Override
	public boolean isWithBall() {
		return true;
	}
	
	public abstract boolean isRight(); 
	
	public abstract void loseTheBall();
	
	// PASES
	public SoccerPlayer getClosestMate(List<SoccerPlayer> mates){
		if(mates.isEmpty())
			return this.getClosestMate();
					
		double minDist = 10000;
		SoccerPlayer closestMate = null;
		for(SoccerPlayer sc : mates){
			if(!(this.getPlayer().equals(sc)) && this.getDistanceWith(this.getPlayer(), sc) < minDist){
				minDist = this.getDistanceWith(this.getPlayer(), sc);
				closestMate = sc;
			}	
		}
		return closestMate;
	}	
	
	public SoccerPlayer getClosestMate() {
		double minDist = 10000;
		SoccerPlayer closestMate = null;
		for(SoccerPlayer sc : this.teammates){
			if(!sc.equals(this.getPlayer()) && this.getDistanceWith(this.getPlayer(), sc) < minDist){
				minDist = this.getDistanceWith(this.getPlayer(), sc);
				closestMate = sc;
			}
		}
		return closestMate;
	}
	
	public double getDistanceWith(SoccerPlayer p1,SoccerPlayer p2){
		return Math.sqrt(Math.pow(p2.getX()-p1.getX(),2) + Math.pow(p2.getY()-p1.getY(),2));
	}
	
	public List<SoccerPlayer> getMatesOnTheLeft(){
		List<SoccerPlayer> result = new ArrayList<SoccerPlayer>();
		for(SoccerPlayer sc : this.teammates){
			if(sc.getX() < this.getPlayer().getX())
				result.add(sc);
		}
		return result;
	}
	
	
	public SoccerPlayer getLeftClosestMate(){
		return this.getClosestMate(this.getMatesOnTheLeft());
	}
	
	public List<SoccerPlayer> getMatesOnTheRight(){
		List<SoccerPlayer> result = new ArrayList<SoccerPlayer>();
		for(SoccerPlayer sc : this.teammates){
			if(sc.getX() > this.getPlayer().getX())
				result.add(sc);
		}
		return result;
	}
	
	public SoccerPlayer getRightClosestMate(){
		return this.getClosestMate(this.getMatesOnTheRight());
	}
	
	public List<SoccerPlayer> getUpMates(){
		List<SoccerPlayer> result = new ArrayList<SoccerPlayer>();
		for(SoccerPlayer sc : this.teammates){
			if(sc.getY() < this.getPlayer().getY())
				result.add(sc);
		}
		return result;
	}
	
	public SoccerPlayer getUpClosestMate(){
		return this.getClosestMate(this.getUpMates());
	}
	
	public List<SoccerPlayer> getDownMates(){
		List<SoccerPlayer> result = new ArrayList<SoccerPlayer>();
		for(SoccerPlayer sc : this.teammates){
			if(sc.getY() > this.getPlayer().getY())
				result.add(sc);
		}
		return result;
	}
	
	public SoccerPlayer getDownClosestMate(){
		return this.getClosestMate(this.getDownMates());
	}
	
	
	
	
	
	
	
	public void setTeammates(List<SoccerPlayer> teammates) {
		this.teammates = teammates;
	}

	public SoccerBall getSoccerBall() {
		return soccerBall;
	}

	public void setSoccerBall(SoccerBall soccerBall) {
		this.soccerBall = soccerBall;
	}

	public List<SoccerPlayer> getTeammates() {
		return teammates;
	}

}
